home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
8174
/
8174.xpi
/
chrome
/
antbar.jar
/
content
/
lib
/
lib.js
< prev
Wrap
Text File
|
2009-12-30
|
9KB
|
346 lines
/*
* DO NOT EDIT THIS FILE !
*
* IT WAS AUTOMATICALLY COPIED FROM THE /lib/ DIRECTORY
* TO UPDATE IT YOU NEED TO BUILD THE XPI OF THE CURRENT PROJECT
*
*/
//
// lib.js
// firefox
//
// Created by Zak on 2008-06-12.
// Copyright 2008 Ant.com. All rights reserved.
//
var AntLibVersion = 1;
var AntLib = (typeof(AntLib) == 'undefined' || AntLib.version < AntLibVersion) ?
{
JSON:null,
toLog: function (str)
{
var self = AntLib;
var consoleService = self.CCSV("@mozilla.org/consoleservice;1", "nsIConsoleService");
consoleService.logStringMessage("AntLog: " + str);
},
openURL: function (url /* , newtab */)
{
if (arguments.length > 1 && arguments[1])
getBrowser().selectedTab = getBrowser().addTab(url);
else
{
window._content.document.location = url;
window.content.focus();
}
},
uriEncode: function (terms)
{
return encodeURI(terms).replace(/\//g, "%2F");
},
openWindow: function (windowType, url, features, params)
{
var self = AntLib;
var wm = self.CCSV("@mozilla.org/appshell/window-mediator;1", "nsIWindowMediator");
var win = windowType ? wm.getMostRecentWindow(windowType) : null;
if (win)
{
if ("initWithParams" in win)
win.initWithParams(aParams);
win.focus();
}
else
{
var winFeatures = "resizable,dialog=no,centerscreen" + (features != "" ? ("," + features) : "");
var parentWindow = (self.instantApply || !window.opener || window.opener.closed) ? window : window.opener;
win = parentWindow.openDialog(url, "_blank", winFeatures, params);
}
return win;
},
compareFFVersion: function (versionString)
{
var self = AntLib;
var appInfo = self.CCSV("@mozilla.org/xre/app-info;1", "nsIXULAppInfo");
var versionChecker = self.CCSV("@mozilla.org/xpcom/version-comparator;1", "nsIVersionComparator");
return (versionChecker.compare(appInfo.version, versionString) >= 0);
},
getSiteName: function (locationObj)
{
var self = AntLib;
var hostname = self.safeGet(locationObj, "hostname");
var index;
index = hostname.lastIndexOf(".");
if (index > -1)
hostname = hostname.substring(0, index);
index = hostname.lastIndexOf(".");
if (index > -1)
hostname = hostname.substring(index + 1);
return hostname;
},
getDomain: function (strURL)
{
var domain;
var self = AntLib;
try
{
var ios = self.CCSV("@mozilla.org/network/io-service;1", "nsIIOService");
var uri = ios.newURI(strURL, null, null);
try {
// Only available in Firefox 3
var eTLDService = self.CCSV("@mozilla.org/network/effective-tld-service;1", "nsIEffectiveTLDService");
domain = eTLDService.getBaseDomain(uri);
self.toLog('eTLDService domain: ' + domain + ' from uri: ' + uri.spec);
} catch (e) {
self.toLog('eTLDService error: ' + e);
var host = uri.host;
var TLD = host.substring(host.lastIndexOf('.')+1, host.length);
var hostNoTLD = host.substring(0, host.lastIndexOf('.'));
var secLevDomain = hostNoTLD.substring(hostNoTLD.lastIndexOf('.')+1, hostNoTLD.length);
domain = secLevDomain+'.'+TLD;
}
} catch (e) {
return null;
}
return domain;
},
/**
* Get main browser window, use in order to bypass security context
*/
getMainWindow: function ()
{
var mainWindow = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIWebNavigation)
.QueryInterface(Components.interfaces.nsIDocShellTreeItem)
.rootTreeItem
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIDOMWindow);
return mainWindow;
},
/**
* Returns "WINNT" on Windows Vista, XP, 2000, and NT systems
* "Linux" on GNU/Linux; and "Darwin" on Mac OS X.
* @return string The Name of the opertating system
*/
getOsName: function ()
{
var self = AntLib;
return self.CCSV("@mozilla.org/xre/app-info;1", "nsIXULRuntime").OS;
},
/**
* Converts a number of bytes to the appropriate unit that results in a
* number that needs fewer than 4 digits
*
* NB: The commented part is only available on FF3
*
* @param aBytes
* Number of bytes to convert
* @return A pair: [new value with 3 sig. figs., its unit]
*/
convertByteUnits: function (aBytes)
{
var unitIndex = 0;
var units = ["B", "KB", "MB", "GB"];
while ((aBytes >= 999.5) && (unitIndex < units.length - 1))
{
aBytes /= 1024;
unitIndex++;
}
aBytes = aBytes.toFixed((aBytes > 0) && (aBytes < 100) ? 1 : 0);
return [aBytes, units[unitIndex]];
},
/*
* Open a file using external protocol service
*/
openExternal: function (aFile)
{
var uri = Cc["@mozilla.org/network/io-service;1"].
getService(Ci.nsIIOService).newFileURI(aFile);
var protocolSvc = Cc["@mozilla.org/uriloader/external-protocol-service;1"].
getService(Ci.nsIExternalProtocolService);
protocolSvc.loadUrl(uri);
},
/**
* Trim a string
*/
trim: function (str)
{
var r = str;
if (!r)
return '';
r = r.replace(/^\s+/, '');
r = r.replace(/\s+$/, '');
r = r.replace(/\s+/g, ' ');
return r;
},
/**
* Sanitize a string
*/
sanitize: function (str)
{
var self = AntLib;
return self.trim(str).replace(/[\\\/\?%\*:\|\"\<\>]/gi, '').replace(/[ _]+/g, '_');
},
upFirstLetter: function (str)
{
return str.substr(0, 1).toUpperCase() + str.substr(1);
},
/**
* Safely get a property using try/catch block
* @param obj The object to get the property from
* @param prop The property to get
* @return property The value or an empty string
*/
safeGet: function(obj, prop)
{
var property;
try
{
property = obj[prop];
}
catch (e)
{
property = '';
}
return property;
},
/* This function returns the user profile folder */
getProfileFolder: function()
{
var p;
var NSIFILE = Components.interfaces.nsIFile;
var dirLocator = Components.classes["@mozilla.org/file/directory_service;1"]
.getService(Components.interfaces.nsIProperties);
p = dirLocator.get("ProfD", NSIFILE).path;
var dirLocal = Components.classes["@mozilla.org/file/local;1"]
.createInstance(Components.interfaces.nsILocalFile);
dirLocal.initWithPath(p);
if (dirLocal.exists() && dirLocal.isDirectory()) {
return dirLocal;
}
return null;
},
_CI: Components.interfaces,
_CC: Components.classes,
/**
* Helpers to acces Mozilla Interfaces, Classes and Services
*/
CC: function(cName)
{
var self = AntLib;
return self._CC[cName];
},
CI: function(ifaceName)
{
var self = AntLib;
return self._CI[ifaceName];
},
CCSV: function(cName, ifaceName)
{
var self = AntLib;
return self._CC[cName].getService(self._CI[ifaceName]);
},
CCIN: function(cName, ifaceName)
{
var self = AntLib;
return self._CC[cName].createInstance(self._CI[ifaceName]);
},
QI: function(obj, iface)
{
return obj.QueryInterface(iface);
},
GI: function (obj, iface)
{
try { return obj.getInterface(iface); }
catch (e) { if (e.name == "NS_NOINTERFACE") {} }
return null;
},
/**
* Abstract for the getElementById function
* @param id The id to look for
* @param doc (Optional) Search in the specified scope instead of document
*/
ob: function (id, doc)
{
var self = AntLib;
var ret;
if (doc == undefined)
ret = document.getElementById(id);
else
ret = doc.getElementById(id);
if (ret == null)
self.toLog("Missing id: " + id + " caller: " + self.ob.caller);
return ret;
}
} : AntLib;
AntLib.toLog("JSON initializing for AntLib version " + AntLibVersion);
/**
* Compatibility for the JSON
*/
if (typeof(JSON) == "undefined") {
try {
// This eval is for avoiding a Rhino syntax error
eval('Components.utils.import("resource://gre/modules/JSON.jsm");');
AntLib.JSON = {
parse: JSON.fromString,
stringify: JSON.toString
};
}
catch (e) {
AntLib.JSON = {};
AntLib.toLog("JSON init error: " + e);
}
}
else {
AntLib.JSON = JSON;
}